package com.mindskip.xzs.controller.student;

import cn.hutool.core.collection.CollUtil;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.ExamPaperAnswerInfo;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum;
import com.mindskip.xzs.service.ExamPaperAnswerService;
import com.mindskip.xzs.service.ExamPaperService;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.question.QuestionPageRequestVM;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageResponseVM;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.viewmodel.student.exam.ExamTrainPageVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;

@RestController("StudentExamPaperController")
@RequestMapping(value = "/api/student/exam/paper")
public class ExamPaperController extends BaseApiController {

    private final ExamPaperService examPaperService;
    private final ExamPaperAnswerService examPaperAnswerService;
    private final ApplicationEventPublisher eventPublisher;

    @Autowired
    public ExamPaperController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService, ApplicationEventPublisher eventPublisher) {
        this.examPaperService = examPaperService;
        this.examPaperAnswerService = examPaperAnswerService;
        this.eventPublisher = eventPublisher;
    }


    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> select(@PathVariable Integer id) {
        User user = getCurrentUser();
        ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        //试卷名称
        String name = vm.getName();
//      //判断是否是正式考试
        if (4 == vm.getPaperType()) {
            Boolean flag = examPaperAnswerService.findExamPaperAnswer(ExamPaperTypeEnum.TimeLimit.getCode(), user,name);
            if (!flag) {
                return RestResponse.fail(2, "正式考试试卷不能重复做!");
            }
        }
        return RestResponse.ok(vm);
    }


    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperPageResponseVM>> pageList(@RequestBody @Valid ExamPaperPageVM model) {
        User user = getCurrentUser();
        PageInfo<ExamPaper> pageInfo = new PageInfo<>();
        if (0 == model.getPaperType()) {
            pageInfo = examPaperService.studentPage(model);
        } else {
            model.setLevelId(user.getUserLevel());
            pageInfo = examPaperService.studentPage2(model);
            if (null == pageInfo) {
                return RestResponse.fail(2, "当前用户在该时间不能考试!");
            }
        }
        if(CollUtil.isEmpty(pageInfo.getList())){
            return RestResponse.ok();
        }
        PageInfo<ExamPaperPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(page);
    }


    @RequestMapping(value = "/selectTrain", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> selectTrain(ExamTrainPageVM examTrainPageVM) {
        int questionType = examTrainPageVM.getQuestionType();
        int id = 25;
        //多选题
        if (questionType == 2) {
            id = 26;
        }
        if (questionType == 3) {
            id = 27;
        }
        ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    }

}
